import data from "../../core/cache_data";
import { HttpRequest, HttpRequestContentType } from "../../core/http_request";
import Utils from "../../core/utils";
import { Protocol, ProtocolObjectUserData, ProtocolS2CUploadFile } from "./protocol_dist";

export class HttpUtils {
    static get UserData() {
        let userDataMixed = data.storage.get("user-data", null);
        let userData = new ProtocolObjectUserData();
        userData.fromMixed(userDataMixed);
        return userData;
    }
    static async QPost<T extends Protocol>(action: string, body: Protocol | any = null, rtnCtor: new () => T, contentType: HttpRequestContentType = "application/json", timeout = 5000) {
        let reqBody = {
            action: action,
            path: "actions"
        };
        if (body instanceof Protocol) {
            body = body.toMixed();
        }
        Object.assign(reqBody, body);
        let url = data.gameConfig.httpConfig.server + "/" + data.gameConfig.httpConfig.apis["REQ"]
        let res: any = await HttpRequest.Post(url, reqBody, contentType, timeout).reqResponse();
        // let res = { data: { __cn: 'S2CIsLogged', is_logged: false }, error: {}, ret: 0 };
        if (!res) {
            console.error("QPost error:", reqBody, rtnCtor);
            return null;
        }
        if (res.ret) {
            console.warn("QPost error:", res.error);
            return null;
        }
        let rtn = new rtnCtor();
        rtn.fromMixed(res.data);
        return rtn;
    }
    static async QGet<T extends Protocol>(action: string, body: Protocol | any = null, rtnCtor: new () => T, contentType: HttpRequestContentType = "application/x-www-form-urlencoded", timeout = 5000) {
        let reqBody = {
            action: action,
            path: "actions"
        };
        if (body instanceof Protocol) {
            body = body.toMixed();
        }
        Object.assign(reqBody, body);
        let url = data.gameConfig.httpConfig.server + "/" + data.gameConfig.httpConfig.apis["REQ"]
        let res: any = await HttpRequest.Get(url, reqBody, contentType, timeout).reqResponse();
        // let res = { data: { __cn: 'S2CIsLogged', is_logged: false }, error: {}, ret: 0 };
        if (!res) {
            console.error("QGet error:", body, rtnCtor);
            return null;
        }
        if (res.ret) {
            console.warn("QGet error:", res.error);
            return null;
        }
        let rtn = new rtnCtor();
        rtn.fromMixed(res.data);
        return rtn;
    }

    static async uploadBlob(dstBlob: Blob, name: string, type: string, replaceOldFileName?: string) {
        let url = data.gameConfig.httpConfig.server + "/" + data.gameConfig.httpConfig.apis["REQ"]
        // 转换为 File 对象
        const dstFile = new File([dstBlob], name, { type: type });
        var formData = new FormData();
        formData.append("action", "upload_file");
        formData.append("path", "actions");
        formData.append("file_info", dstFile);

        if (replaceOldFileName) {
            formData.append("replace", replaceOldFileName);
        }

        let req = HttpRequest.Post(url);
        req.setBody(formData);
        req.contentType = "multipart/form-data";
        let res = await req.reqResponse();
        console.log("res:", res);
        if (res.ret == 1) {
            Utils.game.msgBox("上传错误，创建图片失败");
            return null;
        }
        else {
            let rtn = new ProtocolS2CUploadFile();
            rtn.fromMixed(res.data);
            // Utils.game.msgBox("上传成功");
            return rtn;
        }

    }
}

window["http_utils"]=HttpUtils;